using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for DoProject
/// </summary>
public class DoProject
{
	static public Boolean ExecuteProjectFile(                
            String projectFileName,
                Data.VarParams varParam,
                Data.DataConsolidation dc,
                Data.Data currentData,
                Boolean IsPostBack 
        )
	{
        if (projectFileName == null)
            return false;

        // currentData.SetFileName(varParam.GetMyProjectsDir(), projectFileName);

        //////////////////////////////////////////////////////////////////////////////////////////
        //
        // read and process the project file
        //
        String fullProjectFileName = varParam.GetMyProjectsDir() + projectFileName;
        if ( System.IO.File.Exists(fullProjectFileName))
        {
            String [] projectItems = System.IO.File.ReadAllLines( fullProjectFileName );

            int i;

            if (IsPostBack)
            {
                // Let's scan the stack for relevant operations. We do this by
                // looking backwards in the stack for clearstate operations.
                // Nothing prior to a clearstate needs to be examined.
                for (i = projectItems.Length - 1; i > 0; i--)
                {
                    string thisScriptItem = projectItems[ i ];
                    if (thisScriptItem.ToLower().StartsWith("newstartingpoint"))
                    {
                        i++; // skip to the cmd following the clearstate
                        break;
                    }
                }
            }
            else
            {
                // in this situation, do all the transactions, since there might be
                // embedded Save* commands that post clearstate cmds rely upon.
                i = 0;
            }

            if (i >= 0)
            {
                for (int count = projectItems.Length; i < count; i++)
                {
                    string scriptItem = projectItems[ i ];
                    if ( localRunScript(scriptItem, currentData, true, dc) == false)
                    {
                        varParam.LogError( 
                            "Error",
                            String.Format("The script line {0} failed.", scriptItem));
                        return false;
                    }
                }
            }
        }

        //
        //////////////////////////////////////////////////////////////////////////////////////////

        return true;
	}
    static private bool localRunScript(string thisScriptItem, Data.Data currentData, bool initialLoad, Data.DataConsolidation dc)
    {
        // since items being run by this function do not need to be logged, we could ignore what is being returned by dc.RunScript
        bool success = dc.RunScript(thisScriptItem, currentData, initialLoad);
        return success;
    }

}
